
package com.zhongketech.sunflower.adapter;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.domain.User;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.Parents;
import com.zhongketech.sunflower.utils.AsyncBitmapLoader;
import com.zhongketech.sunflower.utils.AsyncBitmapLoader.ImageCallBack;


/**
 * 简单的好友Adapter实现
 *
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("InflateParams")
public class FcsContactAdapter extends BaseAdapter  {
	
	List<FcsContact> fcList;
	int type;
	Handler handler;
	TextView statusText;
	
	public FcsContactAdapter(List<FcsContact> fcList,int type)
	{
		this.fcList = fcList;
		this.type = type;
		
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if(msg.obj != null)
				{
					MyToast.getInstance().show((String) msg.obj);
					notifyDataSetChanged();
				}

			}
		};
	}
	
	public void updateData(List<FcsContact> fcList,int type)
	{
		this.fcList = fcList;
		this.type = type;
		for(int i = 0; i < fcList.size();i ++)
		{
			LogUtil.i("ZK",fcList.get(i).itemText);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fcList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return fcList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(null == convertView)
		{
			holder = new ViewHolder(); 
			LayoutInflater li = (LayoutInflater) MyApplication.getInstance().getApplicationContext().getSystemService(Service.LAYOUT_INFLATER_SERVICE);
			convertView =  li.inflate(R.layout.fcs_contacts_item, null);
			holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
			holder.itemlayout = (LinearLayout) convertView.findViewById(R.id.itemlayout);
			holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
			holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
			holder.statusLayout = (LinearLayout) convertView.findViewById(R.id.statusLayout);
			holder.statusText = (TextView) convertView.findViewById(R.id.statusText);
			holder.statusText.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(type == Constant.FCS_ADAPTER_TYPE_BIND)
					{
						new Thread(new Runnable() {
							public void run() {
								Looper.prepare();
								boolean ret = new Action().requestsState(fcList.get(position).id, Constant.REQUESTS_STATE_ACCEPT);
								if(ret)
								{
									fcList.get(position).statusText = "已接受";
									final Message msg = new Message();
									msg.obj = "已接受";
									handler.sendMessage(msg);
								}
								else
								{
//									MyToast.getInstance().show("无法绑定");
								}


							}
						}).start();
						
						
					}
				}
			});
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder)convertView.getTag();
		}
		
		if(Constant.FCS_ADAPTER_IMAGE_GONE == fcList.get(position).imageViewId)
		{
			holder.imageView.setVisibility(View.GONE);
		}
		else if(Constant.FCS_ADAPTER_IMAGE_USEDRAW == fcList.get(position).imageViewId)
		{
			holder.imageView.setBackground(fcList.get(position).imageView);
			holder.imageView.setVisibility(View.VISIBLE);
		}
		else
		{
			holder.imageView.setBackgroundResource(fcList.get(position).imageViewId);
			holder.imageView.setVisibility(View.VISIBLE);
		}
		
		if(null != fcList.get(position).imageUrl && fcList.get(position).imageUrl.length() > 1 && fcList.get(position).imageUrl.startsWith("http"))
		{
			AsyncBitmapLoader.getInstance().loadBitmap(holder.imageView, fcList.get(position).imageUrl, new ImageCallBack() {
				
				@Override
				public void imageLoad(ImageView imageView, Bitmap bitmap) {
					// TODO Auto-generated method stub
					if(null != bitmap)
					{
						imageView.setImageBitmap(bitmap);
					}
				}
			});
		}
		
		holder.textView1.setText(fcList.get(position).itemText);

		if(null != fcList.get(position).textview2)
		{
			holder.textView2.setText(fcList.get(position).textview2);	
			holder.textView2.setVisibility(View.VISIBLE);
		}
		else
		{
			holder.textView2.setVisibility(View.GONE);
		}


		holder.statusText.setText(fcList.get(position).statusText);
		statusText = holder.statusText;
		if("接受".equals(fcList.get(position).statusText))
		{
			holder.statusText.setBackgroundColor(Color.GREEN);
		}
		else if(null == fcList.get(position).statusText)
		{
			holder.statusText.setBackgroundResource(R.drawable.phone_action);
		}
		else 
		{
			holder.statusText.setBackgroundColor(Color.TRANSPARENT);
		}
		
		return convertView;
	}

	public final class ViewHolder {
		ImageView imageView;
		LinearLayout itemlayout;
		TextView textView1;
		TextView textView2;
		LinearLayout statusLayout;
		TextView statusText;
	}

}
